package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.SpecialCargoCasErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.SpecialCargoCasConvert;
import com.cmc.cloud.cmclink.doc.entity.SpecialCargoCasDO;
import com.cmc.cloud.cmclink.doc.mapper.SpecialCargoCasMapper;
import com.cmc.cloud.cmclink.doc.service.SpecialCargoCasService;
import com.cmc.cloud.cmclink.doc.vo.specialcargocasvo.SpecialCargoCasBaseVO;
import com.cmc.cloud.cmclink.doc.vo.specialcargocasvo.SpecialCargoCasPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.specialcargocasvo.SpecialCargoCasUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;


/**
 * 特种货申请dg下cas信息 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class SpecialCargoCasServiceImpl implements SpecialCargoCasService {

    @Resource
    private SpecialCargoCasMapper specialCargoCasMapper;
    private static final Pattern CAS_NO_PATTERN = Pattern.compile("\\d{2,7}-\\d{2}-\\d{1}");

    @Override
    public void createSpecialCargoCas(SpecialCargoCasBaseVO createReqVO) {
        // 插入
        SpecialCargoCasDO specialCargoCas = SpecialCargoCasConvert.INSTANCE.convert(createReqVO);
        specialCargoCasMapper.insert(specialCargoCas);
    }

    @Override
    public void updateSpecialCargoCas(SpecialCargoCasUpdateReqVO updateReqVO) {
        // 更新
        SpecialCargoCasDO updateObj = SpecialCargoCasConvert.INSTANCE.convert(updateReqVO);
        specialCargoCasMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpecialCargoCas(Long id) {
        // 删除
        specialCargoCasMapper.deleteById(id);
    }

    @Override
    public void checkCasNo(List<String> casNos) {
        if (CollectionUtil.isEmpty(casNos)) {
            throw ServiceExceptionUtil.exception(SpecialCargoCasErrorCodeConstants.CAS_NO_EMPTY_ERROR);
        }
        Set<String> uniqueCasNos = new HashSet<>(casNos);
        if (uniqueCasNos.size() != casNos.size()) {
            throw ServiceExceptionUtil.exception(SpecialCargoCasErrorCodeConstants.CAS_NO_SAME_ERROR);
        }
        casNos.forEach(cas -> {
            if (!CAS_NO_PATTERN.matcher(cas).matches()) {
                throw ServiceExceptionUtil.exception(SpecialCargoCasErrorCodeConstants.CAS_NO_FORMAT_ERROR);
            }
            String[] casNo = cas.split("-");
            String sequence = casNo[0] + casNo[1];
            int sum = 0;
            for (int i = 0; i < sequence.length(); i++) {
                int digit = Character.getNumericValue(sequence.charAt(sequence.length() - 1 - i));
                sum += digit * (i + 1);
            }
            if (sum % 10 != Integer.parseInt(casNo[2])) {
                throw ServiceExceptionUtil.exception(SpecialCargoCasErrorCodeConstants.CAS_NO_VALID_ERROR);
            }
        });
    }

    @Override
    public SpecialCargoCasDO getSpecialCargoCas(Long id) {
        return specialCargoCasMapper.selectById(id);
    }

    @Override
    public List<SpecialCargoCasDO> getSpecialCargoCasList(Long dgId) {
        LambdaQueryWrapperX<SpecialCargoCasDO> queryWrapperX = new LambdaQueryWrapperX<SpecialCargoCasDO>()
                .eqIfPresent(SpecialCargoCasDO::getSpecialCargoDgId, dgId);
        return specialCargoCasMapper.selectList(queryWrapperX);
    }

    @Override
    public PageResult<SpecialCargoCasDO> getSpecialCargoCasPage(SpecialCargoCasPageReqVO reqVO) {
        LambdaQueryWrapperX<SpecialCargoCasDO> queryWrapperX = new LambdaQueryWrapperX<SpecialCargoCasDO>()
                .eqIfPresent(SpecialCargoCasDO::getSpecialCargoDgId, reqVO.getSpecialCargoDgId());
        return specialCargoCasMapper.selectPage(reqVO, queryWrapperX);
    }
}
